package org.jeecg.modules.demo.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.Constants;
import org.jeecg.modules.demo.annualkeywork.entity.AnnualKeyWork;
import org.jeecg.modules.demo.annualkeywork.entity.KeyWorkDepartments;
import org.jeecg.modules.demo.annualkeywork.entity.ReportAnnualKeyWork;
import org.jeecg.modules.demo.annualkeywork.service.IAnnualKeyWorkService;
import org.jeecg.modules.demo.annualkeywork.service.IKeyWorkDepartmentsService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.quartz.Job;

import java.util.Date;
import java.util.List;
import java.util.UUID;


@Slf4j
@Component
public class ReportAnnualKeyWorkJob implements Job{
    @Autowired
    private  IAnnualKeyWorkService annualKeyWorkService;
    @Autowired
    private IKeyWorkDepartmentsService keyWorkDepartmentsService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("开始创建汇报任务");
        System.out.println("开始创建审批任务");
        /*
        * 每个月的25号定时处理汇报任务和审批任务。
        * 汇报任务根据a的id和k的main_id进行表连接，查找状态为待汇报或者已汇报的记录，然后生成汇报任务。
        * */
        //获取到所有的AnnualKeyWork的id
        List<AnnualKeyWork> AnnualKeyWorkList = annualKeyWorkService.getAllList();
        //获取到所有的KeyWorkDepartments
        List<KeyWorkDepartments> KeyWorkDepartmentsList = keyWorkDepartmentsService.getAllList();
        System.out.println("获取到所有的AnnualKeyWork"+AnnualKeyWorkList);
        for(AnnualKeyWork a : AnnualKeyWorkList){
            //根据AnnualKeyWork的id获取到KeyWorkDepartments的main_id，
            // 然后找状态为待汇报或者已汇报的记录。
            //看其完成进度是否达到100%，达到continue,否则创建。
            List<String> completionProgressList =keyWorkDepartmentsService.SelectCompletionProgress(a.getId());
            System.out.println("获取到完成情况"+completionProgressList);
            ReportAnnualKeyWork reportAnnualKeyWork = new ReportAnnualKeyWork();
            Date date = new Date();
            int wheatherCreateReportAnnualKeyWork=0;
            for(String completionProgress : completionProgressList){
                if(completionProgress.equals("100%")){
                    wheatherCreateReportAnnualKeyWork=1;
                }
            }
            if(wheatherCreateReportAnnualKeyWork!=1) {
                //对各种字段进行赋值
                System.out.println(Constants.START_ASSIGN);
                reportAnnualKeyWork.setId(UUID.randomUUID().toString());
                reportAnnualKeyWork.setUndertaketasks(a.getUndertaketasks());
                reportAnnualKeyWork.setReportMonth(String.valueOf(date.getMonth()+1));
                reportAnnualKeyWork.setTaskstatus(Constants.TOBE_REPORT);
                reportAnnualKeyWork.setJobcontent(a.getJobcontent());
                reportAnnualKeyWork.setMainId(a.getId());
                System.out.println(reportAnnualKeyWork);
                //一些赋值语句
                keyWorkDepartmentsService.saveReport(reportAnnualKeyWork);
                System.out.println(Constants.SAVE_SUCCESS);
            }
        }
        for(int i=0;i<KeyWorkDepartmentsList.size();i++){
            //去找状态为已汇报的记录。
            List<KeyWorkDepartments> KeyWorkList=keyWorkDepartmentsService.SelectFinishedReportAnnualKeyWork();
            for(KeyWorkDepartments k:KeyWorkList)
            {
                if(k.getTaskstatus().equals(Constants.REPORTED)){
                    keyWorkDepartmentsService.updateTaskstatusByKid(k.getId());
                }
            }
        }
    }
}
